package com.gtt.lazy.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.type.Type;

import com.gtt.lazy.domain.Category;
import com.gtt.lazy.service.CategoryService;
import com.gtt.lazy.util.HibernateTemplateUtil;

/**
 * 类别服务实现
 * 
 * @author 高甜甜
 * @date 2009-7-7
 */
public class CategoryServiceImpl implements CategoryService {

	private static final Logger logger = Logger
			.getLogger(CategoryServiceImpl.class);

	private HibernateTemplateUtil hibernateTemplateUtil;

	public int add(Category category) {
		if (logger.isDebugEnabled())
			logger.debug("add Category");
		return (Integer) hibernateTemplateUtil.save(category);
	}

	public Category get(int id) {
		return (Category) hibernateTemplateUtil.get(Category.class, id);
	}

	public void delete(int id) {
		if (logger.isDebugEnabled())
			logger.debug("delete CategoryToSofa [ " + id + ":* ]");
		String hql = "delete from CategoryToSofa as cts where cts.cid=?";
		hibernateTemplateUtil.update(hql, new Object[] { id },
				new Type[] { Hibernate.INTEGER });

		if (logger.isDebugEnabled())
			logger.debug("delete Category [ " + id + " ]");
		hibernateTemplateUtil.delete(Category.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Category> list() {
		String hql = "from Category as c order by c.id desc";
		return hibernateTemplateUtil.find(hql);
	}

	@SuppressWarnings("unchecked")
	public List<Category> list(int start, int size) {
		String hql = "from Category as c order by c.id desc";
		return hibernateTemplateUtil.find(hql, start, size);
	}

	public void update(Category category) {
		if (logger.isDebugEnabled())
			logger.debug("update Category " + category.toString());
		hibernateTemplateUtil.update(category);
	}

	public int count() {
		String hql = "select count(*) from Category as c";
		Long l = (Long) hibernateTemplateUtil.find(hql).get(0);
		return l.intValue();
	}

	public void setHibernateTemplateUtil(
			HibernateTemplateUtil hibernateTemplateUtil) {
		this.hibernateTemplateUtil = hibernateTemplateUtil;
	}

}
